package com.steak.concurrent;
/**
 * @author 刘牌
 * @date 2022-04-1522:32
 */
public class ThreadTest {
    public static void main(String[] args) {
        Object obj = new Object();
        new Thread(() -> {
            for (int i = 1 ; i < 101 ; i++){
                if (i % 2 != 0){
                    synchronized (obj){
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }else {
                    synchronized (obj){
                        obj.notify();
                        System.out.println(Thread.currentThread().getName()+"  "+i);
                    }
                }
            }
        }).start();

        new Thread(() -> {
            for (int i = 1 ; i < 101 ; i++){
                if (i % 2 == 0){
                    synchronized (obj){
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }else {
                    synchronized (obj){
                        obj.notify();
                        System.out.println(Thread.currentThread().getName()+"  "+i);
                    }
                }
            }
        }).start();
    }
}
